package com.asktgapp.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.widget.PswInputView;

/**
 * time:2018/1/25
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

@SuppressLint("ValidFragment")
public class WithdrawDiolog extends DialogFragment implements View.OnClickListener {
    public static final String TAG = WithdrawDiolog.class.getSimpleName();
    private onResult result;
    private TextView mBack;
    private PswInputView inputView;

    @SuppressLint("ValidFragment")
    public WithdrawDiolog(onResult result) {
        this.result = result;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                result.onCancel();
                dismiss();
                break;
        }

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //fragment的视图
        dialog.setContentView(R.layout.dialog_with_draw);
        mBack = dialog.findViewById(R.id.tv_back);
        mBack.setOnClickListener(this);
        inputView = dialog.findViewById(R.id.inputview);
        inputView.setInputCallBack(new PswInputView.InputCallBack() {
            @Override
            public void onInputFinish(String result) {
                doNext(result);
            }
        });
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        WindowManager.LayoutParams params = window.getAttributes();
        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        window.setBackgroundDrawableResource(R.drawable.dialog_bg);
        Display display = windowManager.getDefaultDisplay();
        params.width = display.getWidth();
        params.gravity = Gravity.BOTTOM;
        params.windowAnimations = R.style.popwin_anim_style;
        window.setAttributes(params);
        return dialog;
    }

    private void doNext(String string) {
//        Apiservice apiservice = ApiUtil.getInstance().create();
//        Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show();
        result.onSuccess(string);
        dismiss();

    }

    public interface onResult {
        void onSuccess(String s);

        void onCancel();
    }

}
